package no.ntnu.fp.controllers;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import no.ntnu.fp.storage.DBConnection;

import acm.program.ConsoleProgram;

public abstract class AbstractController {
	ResultSet rs;
	
	protected void setResultSet(ResultSet rs) {
		this.rs = rs;
	}
	
	protected String getString(String string) {
		try {
			return rs.getString(string);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	protected boolean next() {
		try {
			return rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	protected void beforeFirst() {
		try {
			rs.beforeFirst();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

	protected int getInt(String string) {
		try {
			return rs.getInt(string);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	protected boolean isAfterLast() {
		try {
			return this.rs.isAfterLast();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	protected void messageDialog(ConsoleProgram caller, String message) {
		JOptionPane.showMessageDialog(caller, message);
	}
	
	protected void closeDBConnection(DBConnection db) {
		try {
			db.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
